package booklib.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import booklib.dao.BookTypeDao;
import booklib.model.BookType;
import booklib.util.DbUtil;
import booklib.util.StringUtil;

public class BookTypeAddInterFrame extends JInternalFrame {
	private JTextField bookTypeNametxt;
	private JTextArea bookTypeDesctxt;
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private JTextField idtxt;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookTypeAddInterFrame frame = new BookTypeAddInterFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookTypeAddInterFrame() {
		setClosable(true);
		setIconifiable(true);
		setTitle("图书类别添加");
		setBounds(100, 100, 448, 362);
		
		JLabel lblNewLabel = new JLabel("图书类别名称：");
		lblNewLabel.setFont(new Font("楷体", Font.BOLD, 17));
		
		JLabel lblNewLabel_1 = new JLabel("图书类别描述：");
		lblNewLabel_1.setFont(new Font("楷体", Font.BOLD, 17));
		
		bookTypeNametxt = new JTextField();
		bookTypeNametxt.setColumns(10);
		
		bookTypeDesctxt = new JTextArea();
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 15));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeAddActionPerformed(e);
			}
		});
		btnNewButton.setIcon(new ImageIcon(BookTypeAddInterFrame.class.getResource("/images/play_24px_27420_easyicon.net.png")));
		
		JButton btnNewButton_2 = new JButton("重置");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}

		});
		btnNewButton_2.setFont(new Font("宋体", Font.BOLD, 15));
		btnNewButton_2.setIcon(new ImageIcon(BookTypeAddInterFrame.class.getResource("/images/reset_24px_28025_easyicon.net.png")));
		
		idtxt = new JTextField();
		idtxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("图书类别编号:");
		lblNewLabel_2.setFont(new Font("楷体", Font.BOLD, 17));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(17)
							.addComponent(btnNewButton)
							.addPreferredGap(ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
							.addComponent(btnNewButton_2)
							.addGap(67))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
									.addComponent(lblNewLabel_1)
									.addComponent(lblNewLabel_2))
								.addComponent(lblNewLabel))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(bookTypeNametxt)
								.addComponent(bookTypeDesctxt, GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
								.addComponent(idtxt))
							.addContainerGap(24, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(idtxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookTypeNametxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel))
					.addGap(23)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookTypeDesctxt, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1))
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton))
					.addGap(27))
		);
		getContentPane().setLayout(groupLayout);
		bookTypeDesctxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.setLocation((screenWidth-400)/2, (screenHeight-400)/2);
	}
	/**
	 * 图书类别添加事件处理
	 * @param e
	 */
	private void bookTypeAddActionPerformed(ActionEvent evt) {
		// TODO 自动生成的方法存根
		String iid=this.idtxt.getText();
		if(StringUtil.isEmpty(iid)){
			JOptionPane.showMessageDialog(null,"图书类别编号不能为空！");
			return;
		}
		int id=0;
		try{
			id=Integer.valueOf(iid);
		}
		catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"图书类别编号必须为数字！");
			return;
		}
		String bn=this.bookTypeNametxt.getText();
		String bd=this.bookTypeDesctxt.getText();
		if(StringUtil.isEmpty(bn)){
			JOptionPane.showMessageDialog(null,"图书类别名称不能为空！");
			return;
		}
		BookType booktype=new BookType(id,bn,bd);
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int n=bookTypeDao.add(con, booktype);
			if(n==1){
				JOptionPane.showMessageDialog(null,"图书添加成功！");
			}
			else JOptionPane.showMessageDialog(null,"图书添加失败！");
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,"图书添加失败！");
		}finally{
			try { 
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	/**
	 * 重置事件处理
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		this.resetValue();
	}
	/**
	 * 重置表
	 */
	private void resetValue(){
		this.idtxt.setText("");
		this.bookTypeDesctxt.setText("");
		this.bookTypeNametxt.setText("");
	}
}
